package com.dubbo.consumer.proxy;


import com.dubbo.provider.config.HttpProtocol;
import com.dubbo.provider.config.Protocol;
import com.dubbo.provider.config.ProtocolFactory;
import com.dubbo.provider.entity.Invocation;
import com.dubbo.provider.util.Url;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @description: 接口的代理类
 * @author: 石亚宁
 * @create: 2020-05-29 11:26
 **/

public class  ServicePoxy {
	public  static <T> T getPoxy(Class interfaceClass){
		return (T) Proxy.newProxyInstance(ServicePoxy.class.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				Protocol httpClient= ProtocolFactory.getProtocol();
				Invocation invocation=new Invocation();
				invocation.setInterfaceName(interfaceClass.getName());
				invocation.setMethodName(method.getName());
				invocation.setParameters(args);
				invocation.setParametersTypes(method.getParameterTypes());
				/**
				 * TODO url 是可以从注册中心拿，目前就只是模拟
				 */
				Url url=new Url("http://127.0.0.1",8080);
				return httpClient.send(url,invocation);
			}
		});
	}
}
